// pages/helpsigninlist/helpsigninlist.js
import Dialog from '../../dist/dialog/dialog';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    item: {
      activityId: null,
      ids: [],
      type: 1
    },
    list: []
  },
  onChange: function(event) {
    var that = this;
    var ids = that.data.item.ids;
    var nowIds = event.detail;
    if (1 == that.data.item.type && ids.length == 1) {
      if (0 == nowIds.length) {
        that.toggle("item.ids", event.detail);
      } else {
        nowIds.splice(ids, 1);
        that.toggle("item.ids", nowIds);
      }
    } else {
      that.toggle("item.ids", event.detail);
    }

  },
  onClose: function(event) {
    var that = this;
    if (event.detail === 'confirm') {
      // 异步关闭弹窗
      that.toggle("show", false);
      that.submitData();
    } else {
      that.toggle("show", false);
    }
  },
  submitBtn: function() {
    var that = this;
    if (that.data.item.type == 1) {
      this.setData({
        show: true
      })
    } else {
      that.submitData();
    }

  },
  submitData: function() {
    var that = this;
    var item = that.data.item;
    if (item.ids.length == 0) {
      wx.showToast({
        title: '请选择后提交',
        icon: 'none',
        duration: 2000
      })
    } else {
      app.req({
        url: "wechat/act/participation/" + (item.type == 3 ? "signInByOrganizer" : "updateParticipation"),
        method: "post",
        data: item,
        success: function(res) {
          if (200 == res.code) {
            if (3 == item.type) {
              Dialog.alert({
                message: '签到成功'
              }).then(() => {
                wx.navigateBack({
                  delta: 1
                })
              });
            } else {
              wx.navigateBack({
                delta: 1
              })
            }
          } else {
              wx.showToast({
                  title: res.msg,
                  icon: 'none',
                  duration: 2000
              })
          }
        }
      });
    }

  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var type = options.type * 1;
    that.toggle("item.type", type);
    that.toggle("single", type == 1);
    that.setTitle(type)
    that.toggle("item.activityId", options.id);
    that.loadData();
  },
  setTitle: function(type) {
    var title = "";
    switch (type) {
      case 1:
        //发起人
        title = "更换发起人"
        break;
      case 2:
        //管理者
        title = "添加角色"
        break;
      case 3:
        //签到
        title = "帮助签到"
        break;
    }
    wx.setNavigationBarTitle({
      title: title
    })
  },
  loadData: function() {
    var that = this;
    var data = that.data.item;
    app.req({
      url: "wechat/act/participation/listActParticipantByActId",
      method: "post",
      data: {
        activityId: data.activityId,
        isCheck: 1
      },
      success: function(res) {
        if (200 == res.code) {
          var list = res.data;
          var newList = [];
          for (var i in list) {
            var item = list[i];
            switch (data.type) {
              case 1:
                //发起人
                if (!item.isCreate) {
                  newList.push(item);
                }
                break;
              case 2:
                //管理者
                if (!item.isManage) {
                  newList.push(item);
                }
                break;
              case 3:
                //签到
                if (!item.status) {
                  newList.push(item);
                }
                break;
            }
          }
          that.toggle("list", newList);
        }else {
            wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
            })
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },
})